const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    dataType: '',
    page: 1,
    no_more: false,
    tabList: [],
    isFirst: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取我的团队列表

    this.getTeamNav();
    // this.getTeamList();
  },
  /**
   * 获取我的团队导航
   */
  getTeamNav: function() {
    let _this = this;
    App._get('user.agent.team/lists', {}, function(result) {
      // 
      _this.setData({
        tabList: result.data.tab,
        dataType: _this.data.dataType || result.data.tab[0].key
      });
      wx.nextTick(() => {
        if (_this.data.isFirst) {
          _this.getTeamList();
        }

      })
    });
  },

  /**
   * 获取我的团队列表
   */
  getTeamList: function(isNextPage, page) {
    let _this = this;
    App._get('user.agent.team/lists', {
      key: _this.data.dataType,
      page: page || 1,
    }, function(result) {
      // 创建页面数据
      _this.setData(_this.createData(result.data, isNextPage));
    });
  },

  /**
   * 创建页面数据
   */
  createData: function(data, isNextPage) {
    data['isLoading'] = false;
    // 列表数据
    let dataList = this.data.list;
    if (isNextPage == true && (typeof dataList !== 'undefined')) {
      data.list.data = dataList.data.concat(data.list.data)
    }
    // 设置当前页面标题
    wx.setNavigationBarTitle({
      title: '我的团队'
    });
    // 团队总人数
    console.log(data);
    data['team_total'] = data.list.total;
    // 导航栏数据
    // data['tabList'] = [{
    //   value: 1,
    //   text: data.words.team.words.first.value,
    //   total: data.agent.first_num
    // }];
    // if (data.setting.level >= 2) {
    //   data['tabList'].push({
    //     value: 2,
    //     text: data.words.team.words.second.value,
    //     total: data.agent.second_num
    //   });
    //   data['team_total'] += data.agent.second_num;
    // }
    // if (data.setting.level == 3) {
    //   data['tabList'].push({
    //     value: 3,
    //     text: data.words.team.words.third.value,
    //     total: data.agent.third_num
    //   });
    //   data['team_total'] += data.agent.third_num;
    // }
    // 设置swiper的高度
    this.setSwiperHeight();
    data.isFirst = false;
    return data;
  },

  /**
   * 下拉到底加载数据
   */
  triggerDownLoad: function() {
    // console.log(this.data.list);
    // 已经是最后一页
    console.log(111)
    if (this.data.page >= this.data.list.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    this.getTeamList(true, ++this.data.page);
  },

  /**
   * 设置swiper的高度
   */
  setSwiperHeight: function() {
    console.log(22)
    let isTap = true;
    // 获取系统信息(拿到屏幕宽度)
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = isTap ? Math.floor(rpx * 82) : 0, // tap高度
      peopleHeight = Math.floor(rpx * 65), // people高度
      swiperHeight = systemInfo.windowHeight - tapHeight - peopleHeight; // swiper高度
    this.setData({
      swiperHeight
    });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {
    let _this = this;
    _this.setData({
      dataType: e.target.dataset.current,
      list: {},
      page: 1,
      no_more: false,
      isLoading: true,
    }, function() {
      // 获取我的团队列表
      _this.getTeamList();
    });
  },

})